package com.yztc.xiami_app.ui.activity.user;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.yztc.xiami_app.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bmob.v3.BmobSMS;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.QueryListener;


/**
 * “手机号码注册”页面
 */
public class PhoneRegisterActivity extends AppCompatActivity {

    @BindView(R.id.imageview_back_registor)
    ImageView backIv; //返回键的图标
    @BindView(R.id.textview_register)
    TextView registerTV; //注册的标识
    @BindView(R.id.textview_login)
    TextView loginTv; //登录
    @BindView(R.id.edit_phone)
    EditText phoneEdt; //电话号码输入框
    @BindView(R.id.image_clean_register)
    ImageView cleanIv; //清除内容的图标
    @BindView(R.id.btn_get_code)
    Button getVerifyCode; //获取手机验证码


    private Context mContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_phone_register);

        ButterKnife.bind(this);

        mContext = this;

        initView();
    }
    private void initView() {
        phoneEdt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

            @Override
            public void afterTextChanged(Editable s) {}
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String phoneNumber = phoneEdt.getEditableText().toString();
                if (TextUtils.isEmpty(phoneNumber)) { //当没有输入内容的时候，清空按钮不可见
                    cleanIv.setVisibility(View.INVISIBLE);
                } else {
                    cleanIv.setVisibility(View.VISIBLE);
                }
            }
        });

    }

    @OnClick({R.id.imageview_back_registor, R.id.textview_register, R.id.textview_login,
            R.id.image_clean_register,R.id.btn_get_code})
    public void onClick(View view) {
        Intent intent = new Intent();
        switch (view.getId()) {
            case R.id.imageview_back_registor:
                finish();
                break;
            case R.id.textview_register:
                break;
            case R.id.textview_login:
                intent.setClass(this, LoginActivity.class);
                startActivity(intent);
                break;
            case R.id.image_clean_register:
                //一键清空
                phoneEdt.setText("");
                break;
            case R.id.btn_get_code:
                //获取输入的手机号码
                String phone = phoneEdt.getText().toString();
                //注册
                BmobSMS.requestSMSCode(phone, "模板名称",new QueryListener<Integer>() {
                    @Override
                    public void done(Integer smsId,BmobException e) {
                        if(e == null){//验证码发送成功
                            Log.i("smile", "短信id："+smsId);//用于查询本次短信发送详情
                        }else {
                            Log.i("tag","------------->注册失败");
                        }
                    }
                });
                break;
        }
    }

}
